//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.Generic;
using OpenLS.Core.Serialization;
using OpenLS.Vml.Drawing;
using OpenLS.Vml.Serialization;

namespace OpenLS.Vml
{
    internal class Group : IOfficeXmlSerializable
    {
        internal const string xmlName = "group";
        private List<IOfficeXmlSerializable> Elements = new List<IOfficeXmlSerializable>();
        private AllCoreAttributes _allCore;

        internal AllCoreAttributes AllCoreAttributes
        {
            get
            {
                if (_allCore == null)
                    _allCore = new AllCoreAttributes();
                return _allCore;
            }
        }

        private AllShapeAttributes _allShape;

        internal AllShapeAttributes AllShapeAttributes
        {
            get
            {
                if (_allShape == null)
                    _allShape = new AllShapeAttributes();
                return _allShape;
            }
        }

        #region IOfficeXmlSerializable Members

        public void ReadXml(ReadContext context)
        {
            var c2 = context.SwitchNamespace(VmlConstants.VmlNamespaceUri, false);

            using (var c = c2.Read(xmlName))
            {
                AllCoreAttributes.ReadXml(c);
                AllShapeAttributes.ReadXml(c);

                while (c.ReaderLocalName != null)
                {
                    var e = ReadGroupItem(c);
                    while (e != null)
                    {
                        Elements.Add(e);
                        e = ReadGroupItem(c);
                    }
                }
            }
        }

        public void WriteXml(WriteContext context)
        {
            var c2 = context.Switch(VmlConstants.VmlNamespace);
            using (var c = c2.Write(xmlName))
            {
                AllCoreAttributes.WriteXml(c);
                AllShapeAttributes.WriteXml(c);

                c.WriteElements(Elements);
            }
        }

        #endregion

        internal static IOfficeXmlSerializable ReadGroupItem(ReadContext context)
        {
            if (context.ReaderLocalName == null)
                return null;
            switch (context.ReaderLocalName)
            {
                case xmlName:
                    return context.ReadElement<Group>();
                case "shape":
                    return context.ReadElement<Shape>();
                case "shapetype":
                    return context.ReadElement<ShapeType>();
                case "arc":
                    return context.ReadElement<Arc>();
                case "curve":
                    return context.ReadElement<Curve>();
                case "image":
                    return context.ReadElement<Image>();
                case "line":
                    return context.ReadElement<Line>();
                case "oval":
                    return context.ReadElement<Oval>();
                case "polyline":
                    return context.ReadElement<Polyline>();
                case "rect":
                    return context.ReadElement<Rect>();
                case "roundrect":
                    return context.ReadElement<RoundRect>();
                case "diagram":
                    return context.ReadElement<Diagram>();
            }
            var result = ShapeElement.ReadShapeElement(context);
            if (result != null)
                return null;
            switch (context.ReaderLocalName)
            {
                default:
                    throw new NotImplementedException(context.ReaderLocalName);
            }
        }
    }
}